﻿using Microsoft.AspNetCore.Mvc;
using 天离安全_天擎.Tl_Class_Services;
using 天离安全_天擎.Tl_SerVices;

namespace 天离安全_天擎.Controllers.Crawler_Controller
{
    /// <summary>
    /// 检查域名信息模块
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class CheckDomainInfoController : Controller
    {
        /// <summary>
        /// 请求获取域名IP信息 - 包含真实IP地址和CDNIP地址，状态码：209为CDNIP | 200为真实IP
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("Scan")]
        public async Task<IActionResult> CrawlerScan([FromBody] CheckDomainInfoDefault request)
        {
            // 传出域名
            Tl_Api.CheckDomainInfo_Api.Url = request.Url;
            var analyzer = new Tl_SerVices.CheckDomainInfo_Services();
            await analyzer.CheckDomainPort();

            // CDN
            var cdnip = Tl_Api.CheckDomainInfo_Api.Cip;
            // 真实的IP地址
            var zip = Tl_Api.CheckDomainInfo_Api.Ip;

            var intResultss = new List<string>();
            var intResults = new List<string>();

            if (cdnip.Length == 0 && zip.Length == 0)
            {
                return Ok(new { status = "504" });
            }

            if (cdnip.Length > 0)
            {
                var crawler = cdnip.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var success in crawler)
                {
                    intResults.Add(success);
                }
                return Ok(new { status = "209", cdnip = intResults.Distinct(), datetime = DateTime.Now.ToString() });
            }
            else
            {
                var crawler = zip.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var errors in crawler)
                {
                    intResultss.Add(errors.ToString());
                }
            }

            return Ok(new { status = "200", realip = intResultss, datetime = DateTime.Now.ToString() });
        }
    }
}
